<template>
  <div style="display: inline-block;">
    <el-upload
      :action="$http.adornUrl('/admin/folder/file/upload/'+folderId)"
      :headers="{Authorization: $cookie.get('Authorization')}"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-error="handleUploadError"
      :before-remove="beforeRemove"
      :file-list="fileList"
      multiple
      :show-file-list="false">
      <el-button size="small" type="primary">点击上传</el-button>
    </el-upload>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        resourcesUrl: window.SITE_CONFIG.resourcesUrl
      }
    },
    props: {
      value: {
        default: '',
        type: String
      },
      folderId: ''
    },
    computed: {
      fileList () {
        let res = []
        if (this.value) {
          let fileArray = this.value.split(',')
          for (let i = 0; i < fileArray.length; i++) {
            res.push({name: fileArray[i], url: this.resourcesUrl + fileArray[i], response: fileArray[i]})
          }
        }
        this.$emit('input', this.value)
        return res
      }
    },
    mounted () {
    },
    methods: {
      // 图片上传
      handleUploadSuccess (response, file, fileList) {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            let files = fileList.map(file => {
              return file.response
            }).join(',')
            this.$emit('refreshDataList')
          }
        })
      },
      handleUploadError (err, file, fileList) {
        this.$message({
          message: err,
          type: 'error',
          duration: 1500,
          onClose: () => {
          }
        })
      },
      handleRemove (file, fileList) {
        let files = fileList.map(file => {
          return file.response
        }).join(',')
        this.$emit('change', files)
      },
      beforeRemove (file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`)
      }
    }
  }
</script>

<style lang="scss">
</style>
